import Vue from 'vue'
import router from './router'
import store from './store'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import notification from 'ant-design-vue/es/notification'
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import { ACCESS_TOKEN } from '@/store/mutation-types'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['login', 'register', 'registerResult','forgotPassword','UserInfo','logout'] // no redirect whitelist

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
  console.log('====='+Vue.ls.get(ACCESS_TOKEN))
  //Vue.ls.set(ACCESS_TOKEN, '123', 7 * 24 * 60 * 60 * 1000)
  console.log('====='+Vue.ls.get(ACCESS_TOKEN)+'---'+to.path)
  if (Vue.ls.get(ACCESS_TOKEN)) {
    /* has token */
    if (to.path === '/user/login') {

      next({ path: '/dashboard/analysis' })
      next()
      NProgress.done()
    } else {
    next()
    }
  } else {
    if (whiteList.includes(to.name)) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next()
     // next({ path: '/user/login', query: { redirect: to.fullPath } })
     // NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
